import { access, mkdir } from 'fs/promises';
import { constants } from 'fs';
export const dev = {
    type: 'mysql' as const,
    host: '162.14.65.53',
    port: 3306,
    username: 'liangcheng',
    password: '123456',
    database: 'liangcheng',
    autoLoadEntities: true,
    // synchronize: true,
}

export const tokenKey = "liangcheng No1 ^_^"
export const avartSrc = "http://127.0.0.1:3030/image/"
export const AppID = 'wxdd3eaff772585815'
export const AppSecret = '82c194d68934b6c08ded556d85d77d6c'
export const wxloginUrl = "https://api.weixin.qq.com/sns/jscode2session"
export const wxToken = "https://api.weixin.qq.com/cgi-bin/token"
export const wxuserUrl = (token: string) => {
    return `https://api.weixin.qq.com/cgi-bin/user/info?access_token=${token}&openid=OPENID&lang=zh_CN`
}

/**
 * 确保指定目录存在，如果不存在则创建它
 * @param dirPath 目录路径
 * @returns 返回一个 Promise，resolve 为 true 表示目录存在或已创建，false 表示创建失败
 */
export async function ensureDir(dirPath: string): Promise<boolean> {
    try {
        // 检查目录是否存在
        await access(dirPath, constants.F_OK);
        return true;
    } catch (err) {
        // 如果目录不存在，则创建它
        if (err.code === 'ENOENT') {
            await mkdir(dirPath, { recursive: true });
            return true;
        }
        // 其他错误抛出，以便调用者处理
        throw err;
    }
}

export function success(data: any, msg: string = 'success', code: number = 200) {
    return {
        data,
        msg,
        code,
        timestamp: new Date()
    }
}

export function error(data: any, msg: string = 'error', code: number = -200) {
    return {
        data,
        msg,
        code,
        timestamp: new Date()
    }
}